package com.ktgj.customer.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.TravelCommentPicture;
import com.ktgj.customer.domain.TravelCommentReply;
import com.ktgj.customer.domain.vo.CommentPictureVo;
import com.ktgj.customer.domain.vo.CommentReplyVo;
import com.ktgj.customer.domain.vo.CommentVo;
import com.ktgj.customer.mapper.TravelCommentPictureMapper;
import com.ktgj.customer.mapper.TravelCommentReplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.TravelCommentMapper;
import com.ktgj.customer.domain.TravelComment;
import com.ktgj.customer.service.ITravelCommentService;

import javax.servlet.http.HttpServletRequest;

/**
 * 评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-12
 */
@Service
public class TravelCommentServiceImpl implements ITravelCommentService 
{
    @Autowired
    private TravelCommentMapper travelCommentMapper;

    @Autowired
    private LoginServiceImpl loginService;

    @Autowired
    private TravelCommentPictureMapper travelCommentPictureMapper;

    @Autowired
    private TravelCommentReplyMapper travelCommentReplyMapper;

    /**
     * 查询评论
     * 
     * @param commentId 评论ID
     * @return 评论
     */
    @Override
    public TravelComment selectTravelCommentById(Long commentId)
    {
        return travelCommentMapper.selectTravelCommentById(commentId);
    }

    /**
     * 查询评论列表
     * 
     * @param travelComment 评论
     * @return 评论
     */
    @Override
    public List<TravelComment> selectTravelCommentList(TravelComment travelComment)
    {
        return travelCommentMapper.selectTravelCommentList(travelComment);
    }

    /**
     * 新增评论
     * 
     * @param travelComment 评论
     * @return 结果
     */
    @Override
    public int insertTravelComment(TravelComment travelComment)
    {
        travelComment.setCreateTime(DateUtils.getNowDate().toString());
        return travelCommentMapper.insertTravelComment(travelComment);
    }

    /**
     * 修改评论
     * 
     * @param travelComment 评论
     * @return 结果
     */
    @Override
    public int updateTravelComment(TravelComment travelComment)
    {
        return travelCommentMapper.updateTravelComment(travelComment);
    }

    /**
     * 批量删除评论
     * 
     * @param commentIds 需要删除的评论ID
     * @return 结果
     */
    @Override
    public int deleteTravelCommentByIds(Long[] commentIds)
    {
        return travelCommentMapper.deleteTravelCommentByIds(commentIds);
    }

    /**
     * 删除评论信息
     * 
     * @param commentId 评论ID
     * @return 结果
     */
    @Override
    public int deleteTravelCommentById(Long commentId)
    {
        return travelCommentMapper.deleteTravelCommentById(commentId);
    }

    @Override
    public List<TravelComment> queryTravelCommentList() {
        return travelCommentMapper.queryTravelCommentList();
    }

    @Override
    public List<TravelComment> queryGroupCommentById(Long groupId) {
        return travelCommentMapper.queryGroupCommentById(groupId);
    }

    @Override
    public AjaxResult saveComment(HttpServletRequest request, TravelComment travelComment) {
        Date now = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowStr = dateFormat.format(now);
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        travelComment.setUserId(customer.getCustomerId());
        travelComment.setUserName(customer.getCustomerName());
        travelComment.setFaceurl(customer.getAvatar());
        travelComment.setCreateTime(nowStr);
        travelCommentMapper.insertTravelComment(travelComment);
        List<TravelCommentReply> list =travelComment.getTravelCommentReplyList();
        if (list == null) {
            list = new ArrayList<>();
        }
        TravelCommentReply travelCommentReply = new TravelCommentReply();
        travelCommentReply.setCommentReplyId(travelComment.getCommentReplyId());
        travelCommentReply.setCommentReplyContent(travelComment.getCommentContent());
        travelCommentReply.setUserReplyId(customer.getCustomerId());
        travelCommentReply.setUserReplyName(customer.getCustomerName());
        travelCommentReply.setFaceurl(customer.getAvatar());
        travelCommentReply.setCommentReplyTime(nowStr);
        list.add(travelCommentReply);
        travelComment.setTravelCommentReplyList(list);
        travelCommentMapper.insertTravelComment(travelComment);
        return AjaxResult.success();
    }

    /**
     * 查询指定旅游产品评论信息
     * */
    @Override
    public List<CommentVo> commentVoList(Long groupId) throws Exception {
        if (groupId != null && groupId != 0){
            List<TravelComment> travelComments = travelCommentMapper.selectTravelComment(groupId);
            if (travelComments != null && travelComments.size() > 0){
                List<CommentVo> commentVoList = new ArrayList<>();
                for (TravelComment travelComment : travelComments) {
                    CommentVo commentVo = new CommentVo();
                    commentVo.setCommentId(travelComment.getCommentId());
                    commentVo.setUserId(travelComment.getUserId());
                    commentVo.setUserName(travelComment.getUserName());
                    commentVo.setFaceurl(travelComment.getFaceurl());
                    commentVo.setCommentContent(travelComment.getCommentContent());
                    commentVo.setCommentScore(travelComment.getCommentScore().toString());
                    commentVo.setCreateTime(travelComment.getCreateTime());

                    List<TravelCommentPicture> travelCommentPictures = travelCommentPictureMapper.selectTravelCommentPicture(travelComment.getCommentId());
                    if (travelCommentPictures != null && travelCommentPictures.size() > 0){
                        List<CommentPictureVo> commentPictureVoList = new ArrayList<>();
                        for (TravelCommentPicture travelCommentPicture : travelCommentPictures) {
                            CommentPictureVo commentPictureVo = new CommentPictureVo();
                            commentPictureVo.setPictureUrl(travelCommentPicture.getPictureUrl());
                            commentPictureVoList.add(commentPictureVo);
                        }
                        commentVo.setCommentPictureVoList(commentPictureVoList);
                    }

                    List<TravelCommentReply> travelCommentReplies = travelCommentReplyMapper.travelCommentReplyList(travelComment.getCommentId());
                    if (travelCommentReplies != null && travelCommentReplies.size() > 0){
                        List<CommentReplyVo> commentReplyVoList = new ArrayList<>();
                        for (TravelCommentReply travelCommentReply : travelCommentReplies) {
                            CommentReplyVo commentReplyVo = new CommentReplyVo();
                            commentReplyVo.setCommentReplyId(travelCommentReply.getCommentReplyId());
                            commentReplyVo.setUserReplyId(travelCommentReply.getUserReplyId());
                            commentReplyVo.setUserReplyName(travelCommentReply.getUserReplyName());
                            commentReplyVo.setFaceurl(travelCommentReply.getFaceurl());
                            commentReplyVo.setCommentReplyContent(travelCommentReply.getCommentReplyContent());
                            commentReplyVo.setCommentReplyTime(travelCommentReply.getCommentReplyTime());
                            commentReplyVoList.add(commentReplyVo);
                        }
                        commentVo.setCommentReplyVoList(commentReplyVoList);
                    }
                    commentVoList.add(commentVo);
                }
                return commentVoList;
            }
            else {
                throw new Exception("未查到数据");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }
}
